package com.nuonuo.nuo_xp;

import android.util.Log;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author glsite.com
 * @version $Rev$
 * @des ${TODO}
 * @updateAuthor $Author$
 * @updateDes ${TODO}
 */
public class HTTPS {

    public static String HttpGet(String strUrl){
        try {
            //Log.d("XP","123132112122313213112123");
            //调用方法时,输入的接口
            URL url = new URL(strUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            //设置为get请求
            connection.setRequestMethod("GET");
            //设置连接主机超时时间
            connection.setConnectTimeout(5000);
            //设置从主机读取数据超时
            connection.setReadTimeout(5000);

            int code = connection.getResponseCode();
            //获取请求码 200 300 400 404等 200是网络成功
            System.out.println(code);
            if (code == 200) {
                //字符串拼接 可以用其他方法
                InputStream inputStream = connection.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
                StringBuffer buffer = new StringBuffer();
                String str = "";
                while ((str = reader.readLine()) != null) {
                    buffer.append(str);
                }
                //返回拼接的字符串
                return buffer.toString();
            }
            //释放connection占用的内存
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

public static void main(String[] args) {
    System.out.println(HttpGet("https://www.baidu.com"));
    System.out.println(HttpGet("http://www.baidu.com"));
}






}
